/*
 * @Author: silence
 * @Date: 2023-07-05 16:33:20
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-31 19:32:10
 * @Description: 电站设置-充电时间段
 */
import 'package:ems_neutral_app/common/widgets/cubitWidget/SettingItem.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:ems_neutral_app/utils/color/ColorUtils.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../common/widgets/cubitWidget/BottomTimeSheetView.dart';
import '../Controller/PowerStationController.dart';
import 'PowerStationSetItem.dart';

class PsSetTimePeriodSettingView extends StatefulWidget {
  const PsSetTimePeriodSettingView({super.key});

  @override
  State<PsSetTimePeriodSettingView> createState() =>
      _PsSetTimePeriodSettingViewState();
}

class _PsSetTimePeriodSettingViewState
    extends State<PsSetTimePeriodSettingView> {
  List timeData = [
    '0000',
    '0000',
    '0000',
    '0000',
    '0000',
    '0000',
    '0000',
    '0000'
  ];

  /// 选中的时间列表
  List selectedTimeList = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

  List _timePeriodList = [];

  PowerStationController _powerStationController =
      Get.put(PowerStationController());

  @override
  void initState() {
    super.initState();

    getData();
    _powerStationController.powerStationSetInfo.listen((p0) {
      getData();
      if (this.mounted) setState(() {});
    });
  }

  getData() {
    timeData[0] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_116'];
    timeData[1] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_117'];
    timeData[2] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_118'];
    timeData[3] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_119'];
    timeData[4] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_120'];
    timeData[5] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_121'];
    timeData[6] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_122'];
    timeData[7] =
        _powerStationController.powerStationSetInfo['eybond_ctrl_123'];
  }

  @override
  Widget build(BuildContext context) {
    _timePeriodList = [
      LangOf(context)?.power_setting?.chargeTime1,
      LangOf(context)?.power_setting?.dischargeTime1,
      LangOf(context)?.power_setting?.chargeTime2,
      LangOf(context)?.power_setting?.dischargeTime2,
    ];
    return Column(
      children: [
        timePeriodSettingItem(0),
        timePeriodSettingItem(1),
        timePeriodSettingItem(2),
        timePeriodSettingItem(3),
      ],
    );
  }

  /// 替换成时间格式,返回时间数组【小时，分钟】
  List<String> replaceToTime([String tempStr = '0000']) {
    if (tempStr.length < 4) return ['00', '00'];
    String hours = tempStr.substring(0, 2);
    String minute = tempStr.substring(2, 4);
    return [hours, minute];
  }

  /// 处理小时分钟，返回0000格式
  String handlerTime([int hours = 0, int minute = 0]) {
    String hoursStr = '';
    hoursStr = (hours < 10) ? '0$hours' : '$hours';

    String minuteStr = '';
    minuteStr = (minute < 10) ? '0$minute' : '$minute';
    return '${hoursStr}${minuteStr}';
  }

  /// 充放电时间段设置
  timePeriodSettingItem(int tag) {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 20.w),
      decoration: BoxDecoration(
          border: Border(
              bottom: BorderSide(
                  color: ColorUtils.color_divider_line, width: 0.5.w))),
      padding: EdgeInsets.only(left: 10.w, top: 15.w, bottom: 15.w),
      child: Container(
        child: Row(
          children: [
            Expanded(
                flex: 1,
                child: SilText(
                  _timePeriodList[tag],
                  fontSize: 16.sp,
                  color: ColorUtils.color_font_main,
                  fontWeight: FontWeight.w500,
                  textAlign: TextAlign.left,
                  maxLines: 0,
                )),

            // 右侧
            Expanded(flex: 1, child: rightView(tag)),
            // rightView(tag)
          ],
        ),
      ),
    );
  }

  rightView(int tag) {
    int index = tag * 2;
    String firstTime = timeData[index];
    String firstTimeStr =
        '${replaceToTime(firstTime)[0]}:${replaceToTime(firstTime)[1]}';
    String secondTime = timeData[index + 1];
    String secondTimeStr =
        '${replaceToTime(secondTime)[0]}:${replaceToTime(secondTime)[1]}';
    return Container(
        alignment: Alignment.centerRight,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            SilButton(
              radius: 4.w,
              borderWidth: 1.5.w,
              padding:
                  EdgeInsets.only(left: 3.w, right: 3.w, top: 3.w, bottom: 2.w),
              borderColor: ColorUtils.color_font_minor,
              title: firstTimeStr,
              fontSize: 14.sp,
              titleColor: ColorUtils.color_font_minor,
              onTapFunc: () {
                chooseTime(
                  hours: int.parse(replaceToTime(firstTime)[0]),
                  minute: int.parse(replaceToTime(firstTime)[1]),
                  tapSureSeletedFunc: (p0, p1) {
                    timeData[index] = handlerTime(p0, p1);

                    /// 更新数据
                    int timeKey = 116 + index;
                    _powerStationController.powerStationSetInfo.update(
                        'eybond_ctrl_${timeKey}', (value) => timeData[index]);
                  },
                );
              },
            ),
            SilText(
              '-',
              color: ColorUtils.color_font_minor,
              width: 10.w,
              textAlign: TextAlign.center,
              margin: EdgeInsets.symmetric(horizontal: 5.w),
            ),
            SilButton(
              radius: 4.w,
              borderWidth: 1.5.w,
              padding:
                  EdgeInsets.only(left: 3.w, right: 3.w, top: 3.w, bottom: 2.w),
              borderColor: ColorUtils.color_font_minor,
              title: secondTimeStr,
              fontSize: 14.sp,
              titleColor: ColorUtils.color_font_minor,
              onTapFunc: () {
                chooseTime(
                  hours: int.parse(replaceToTime(secondTime)[0]),
                  minute: int.parse(replaceToTime(secondTime)[1]),
                  tapSureSeletedFunc: (p0, p1) {
                    timeData[index + 1] = handlerTime(p0, p1);

                    /// 更新数据
                    int timeKey = 116 + index + 1;
                    _powerStationController.powerStationSetInfo.update(
                        'eybond_ctrl_${timeKey}',
                        (value) => timeData[index + 1]);
                  },
                );
              },
            ),
          ],
        ));
  }

  /// 选择时间
  chooseTime(
      {int hours = 0,
      int minute = 0,
      void Function(int, int)? tapSureSeletedFunc}) {
    SilBottomSheet.show(
        context: context,
        child: BottomTimeSheetView(
          initialHours: hours,
          initialMinute: hours,
          tapSureSeletedFunc: (hour, minute) {
            if (tapSureSeletedFunc != null) tapSureSeletedFunc(hour, minute);
          },
        ));
  }
}
